﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHelper;
using clf.weighing.entity;


namespace weighing
{
    public class BillImageDao : BaseDao<BillImage>
    {
        #region instance

        private static BillImageDao? instance;
        public static BillImageDao Instance => instance ??= new BillImageDao();

        public override string GetTableName()
        {
            return TableName.bill_image.ToString();
        }

        #endregion

        /// <summary>
        /// 保存的路径
        /// </summary>
        /// <param name="bill"></param>
        /// <param name="fileName">截图保存的路径</param>
        /// <returns></returns>
        public int SaveCuptureImage(string number, String fileName, int position, string positionDes = "")
        {
            WeighingBill bill = WeighingBillDao.Instance.FindByNumber(number);

            BillImage image = new BillImage
            {
                id = IdHelper.LongID,
                billId = bill?.id,
                positon = position,
                billNumber = bill?.number ?? number,
                carId = bill?.carId,
                carNumber = bill?.carNumber,
                isUp = 0,
                addTime = DateTime.Now,
                localPath = fileName.Replace("\\", "\\\\"),
                positionName = positionDes
            };

            return Sqlite.Insert(image);

        }
        /// <summary>
        /// 获取已经上传的图片信息
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public List<BillImage> GetUpDatas(int rows = 1)
        {
            string condition = $"{BillImageField.is_up} = 1";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, "id desc", rows);
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }
        /// <summary>
        /// 获取磅单图片
        /// </summary>
        /// <param name="billId">磅单号</param>
        /// <returns></returns>
        public List<BillImage> SelectByBillNumber(string billNo)
        {
            string condition = $"{BillImageField.bill_number} = '{billNo}'";
            string sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition, null, null, $"{BillImageField.add_time} asc");
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }

        /// <summary>
        /// 获取磅单图片
        /// </summary>
        /// <param name="billId">磅单号</param>
        /// <returns></returns>
        public List<BillImage> SelectByBillId(long billId)
        {
            string condition = $"{BillImageField.bill_id} = {billId}";
            string sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition, null, null, $"{BillImageField.add_time} asc");
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }


        /// <summary>
        /// 获取未上传的图片信息
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public List<BillImage> GetNoUpDatas(int rows = 1)
        {
            string condition = $"{BillImageField.is_up} = 0";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, "id desc,add_time desc", rows);
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }

        /// <summary>
        /// 获取未上传到OOS的　图片信息
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public List<BillImage> GetOOSNoUpDatas(int row = 1)
        {
            string condition = $"{BillImageField.remote_url} is null  or {BillImageField.remote_url} = ''";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, "id desc,add_time desc", row);
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }

        public bool IsScreemExist(Int64 billid)
        {
            string condition = $"{BillImageField.bill_id} = {billid} and {BillImageField.positon} = {(int)CamaraPosition.Screen}";
            string filed = $"{BillImageField.id}";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), filed, condition);
            var data = Sqlite.Select<BillImage>(sql);
            return data.Count > 0;
        }

        /// <summary>
        /// 查询远程址为空的
        /// </summary>
        /// <param name="rows"></param>
        /// <returns></returns>
        public List<BillImage> SelectNoRemoterUrl(int rows)
        {
            //string condition = $" (LENGTH(`{BillImageField.remote_url}`) < 6 or {BillImageField.remote_url} is null) "+
            //    $" and {BillImageField.remote_url} not like '{Constract.ImageFileNotExistTag}%'";
            //    
            string condition = $" {BillImageField.remote_url} is null";
            string sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition,rows);
            var data = Sqlite.Select<BillImage>(sql);
            return data;
        }
    }
}
